package com.itheima.service.db.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itheima.domain.db.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.db.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Service //dubbo的service
public class UserServiceImpl implements UserService {

    @Autowired //直接注入mapper对象
    private UserMapper userMapper;
    //保存对象返回主键
    @Override
    public Long save(User user) {
        System.out.println("服务器段保存的user对象1 保存前:"+user);
        userMapper.insert(user);//保存对象
        System.out.println("服务器段保存的user对象2 保存后:"+user);
        return user.getId();
    }

    //根据手机号查询用户
    @Override
    public User findByPhone(String phone) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone" , phone);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public User findById(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id" , id);
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }


    @Override
    public void updatePhone(Long id, String phone) {
        UpdateWrapper<User> userUpdateWrapper=new UpdateWrapper<>();
        userUpdateWrapper.eq("id",id);
        User user = new User();
        user.setPhone(phone);
        userMapper.update(user,userUpdateWrapper);
    }

}
